package com.apestech.framework.xml.table.modul;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.json.JSONOrder;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2018-12-29-17:52
 */
public class MasterTable {
    public class SubTable{
        private String id;
        private String name;
        private JSONOrder subTable;
        public SubTable(String id, String name){
            this.id=id;
            this.name=name;
        }

        public void addSubTable(SubTable sub){
            if(subTable==null) subTable=new JSONOrder();
            subTable.put(sub.getId(), sub);
        }

        public void setSubTable(JSONOrder subTable) {
            this.subTable = subTable;
        }

        public JSONOrder getSubTable() {
            return subTable;
        }

        public SubTable getSubTable(String key) {
            if(subTable.get(key)==null) return null;
            return (SubTable) subTable.get(key);
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

    private JSONObject path=new JSONObject();
    private SubTable table;
    private String masterTable;
    private JSONObject child=new JSONObject();

    public void addMaster(String tabId, String tabName){
        table=new SubTable(tabId, tabName);
        this.masterTable=tabId;
    }

    public void addDetail(String tabId, String tabName, String master){
        SubTable sub=null;
        String spath="";
        if(master.equals(masterTable)){
            sub=table;
            spath=masterTable;
        } else {
            spath=path.getString(master)+"."+master;
            String[] plist=spath.split("\\.");
            sub=table;
            int len=plist.length;
            for(int i=1; i<len; i++){
                String key=plist[i];
                sub= (SubTable) sub.getSubTable().get(key);
            }
        }
        sub.addSubTable(new SubTable(tabId, tabName));
        path.put(tabId, spath);
        ArrayList<String> list=null;
        if(child.get(master)!=null) list= (ArrayList<String>) child.get(master);
        list=new ArrayList<String>();
        list.add(tabName);
        child.put(sub.getName(), list);
    }

    public SubTable getTable(String id){
        String spath=path.getString(id);
        if(spath==null) return table;
        String[] plist=spath.split("\\.");
        SubTable sub=null;
        sub=table;
        int len=plist.length;
        for(int i=1; i<len; i++){
            String key=plist[i];
            sub= (SubTable) sub.getSubTable().get(key);
        }
        return sub;
    }

//    public JSONObject getTableData(String id, JSONObject data){
//        if(data==null) return null;
//        JSONObject map=new JSONObject();
//        map.putAll(data);
//        String spath=path.getString(id);
//        if(spath==null) return null;
//        String[] plist=spath.split("\\.");
//        SubTable sub=null;
//        sub=table;
//        int len=plist.length;
//        for(int i=1; i<len; i++){
//            String key=plist[i];
//            sub= (SubTable) sub.getSubTable().get(key);
//            if(!sub.getName().equals(id)){
//                JSONObject data.get(key);
//            }
//        }
//        return map;
//    }

    public SubTable getTable() {
        return table;
    }

    public String getMasterTable() {
        return masterTable;
    }

    public JSONObject getChild() {
        return child;
    }
}
